"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DBE_PERF功能类
Case Name   : 事件统计函数及视图验证WALBufferAccess、WALBufferFull结果一致
Create At   : 2024-4-2
Owner       : @peilinqian
Description :
    1、建表
    2、设置wal_buffers参数为32kB，重启数据库
    3、执行大量数据插入
    4、查询get_instr_wait_event()、dbe_perf.wait_events、
        dbe_perf.global_wait_events，WALBuffer相关wait事件统计信息;
Expect      :
    1、建表; 成功
    2、设置wal_buffers参数为32kB，重启数据库; 成功
    3、建表并执行大量数据插入; 成功
    4、查询get_instr_wait_event()、dbe_perf.wait_events、
        dbe_perf.global_wait_events，WALBuffer相关wait事件统计信息;结果一致
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DbeperfWaitEvents(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}: start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.t_name = 't_dbeperf_waitevents0003'

    def test_main(self):
        self.log.info('----查询wal_buffers参数初始值----')
        self.init_para = self.common.show_param('wal_buffers')
        step_txt = '----step1: 建表; expect: 成功----'
        self.log.info(step_txt)
        create_sql = f'drop table if exists {self.t_name};' \
                     f'create table {self.t_name}(id int,name text);'
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, create_result)

        step_txt = '----step2: 设置wal_buffers参数为32kB; expect: 成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("set",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "wal_buffers=32kB")
        self.assertTrue(result, '执行失败:' + step_txt)
        step_txt = '----step2: 重启数据库，使参数生效; expect: 重启成功----'
        self.log.info(step_txt)
        restart_result = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart_result)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('wal_buffers')
        self.assertEqual(self.new_para, '32kB', '执行失败:' + step_txt)

        step_txt = '----step3: 执行大量数据插入; expect: 操作成功----'
        self.log.info(step_txt)
        create_sql = f'insert into {self.t_name} values' \
                     f'(generate_series(1,1000000),generate_series(1,1000000));'
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, create_result,
                      '执行失败:' + step_txt)

        step_txt = '----step4: 查询get_instr_wait_event()、dbe_perf.wait_events、' \
                   'dbe_perf.global_wait_events，WALBuffer相关wait事件统计信息; ' \
                   'expect: 查询结果一致----'
        self.log.info(step_txt)
        check_sql1 = "select * from dbe_perf.wait_events where event " \
                     "in('WALBufferAccess','WALBufferFull') order by event;"
        check_result1 = self.pri_sh.execut_db_sql(check_sql1)
        self.log.info(check_result1)
        check_sql2 = "select * from get_instr_wait_event(0) where event " \
                     "in('WALBufferAccess','WALBufferFull') order by event;"
        check_result2 = self.pri_sh.execut_db_sql(check_sql2)
        self.log.info(check_result2)
        check_sql3 = "select * from dbe_perf.global_wait_events where event " \
                     "in('WALBufferAccess','WALBufferFull') order by event;"
        check_result3 = self.pri_sh.execut_db_sql(check_sql3)
        self.log.info(check_result1)
        self.assertTrue(check_result1 == check_result2 == check_result3,
                        '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step5_txt = '----step5: 清除表数据----'
        self.log.info(step5_txt)
        drop_sql = f'drop table if exists {self.t_name};'
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)

        step6_txt = '----step6: 还原参数----'
        self.log.info(step6_txt)
        set_result = self.pri_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"wal_buffers={self.init_para}")

        step7_txt = '----step7: 重启数据库，使参数生效; expect:重启成功----'
        self.log.info(step7_txt)
        restart_result = self.pri_sh.restart_db_cluster()

        self.log.info(f'----{os.path.basename(__file__)}: end----')
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_result,
                      '执行失败:' + step5_txt)
        self.assertTrue(set_result, '执行失败:' + step6_txt)
        self.assertTrue(restart_result, '执行失败:' + step7_txt)
